package com.maserb.monitor.service.jwEducation.impl;

import com.maserb.monitor.common.CreateTimeUtils;
import com.maserb.monitor.entity.JwEducationClobEntity;
import com.maserb.monitor.entity.JwEducationInfoEntity;
import com.maserb.monitor.repository.JwEducationClobRepository;
import com.maserb.monitor.repository.JwEducationInfoRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.jwEducation.JwEducationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class JwEductionInfoServiceImpl extends JpaSpecificationServiceImpl<JwEducationInfoRepository,
        JwEducationInfoRepository, JwEducationInfoEntity, String> implements JwEducationInfoService{

    @Autowired
    private JwEducationClobRepository clobRepository;

    @Override
    public Page<JwEducationInfoEntity> findAll(Pageable pageable,  String title , String type ,String isRelease,String stauts) {
        return this.findAll(new Specification<JwEducationInfoEntity>() {
            @Override
            public Predicate toPredicate(Root<JwEducationInfoEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathTitle = root.get("title");
                Path<String> pathType = root.get("type");
                Path<String> pathIsRelease = root.get("isRelease");
                Path<String> pathStauts = root.get("stauts");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(title)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathTitle, "%" + title + "%")));
                }
                if (!StringUtils.isEmptyOrWhitespace(type)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathType, type)));
                }
                if (!StringUtils.isEmptyOrWhitespace(isRelease)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathIsRelease, isRelease)));
                }
                if (!StringUtils.isEmptyOrWhitespace(stauts)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathStauts, stauts)));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }

    @Override
    public Page<JwEducationInfoEntity> findAllByType(Pageable pageable, String type) {
        return this.findAll(new Specification<JwEducationInfoEntity>() {
            @Override
            public Predicate toPredicate(Root<JwEducationInfoEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathType = root.get("type");
                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(type)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.equal(pathType,type)));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }

    @Override
    public List<JwEducationInfoEntity> findByTitle(String title) {
        return repository.findByTitle(title);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(JwEducationInfoEntity entity, JwEducationClobEntity clobEntity) {
        if ( null != entity.getId()) {
            JwEducationClobEntity entityClob = this.findOne(entity.getId()).getClob();
            clobRepository.delete(entityClob);
        }
        clobRepository.save(clobEntity);
        this.save(entity);
    }

    //发布
    @Override
    public void doRelease(String id) {
        JwEducationInfoEntity entity = this.findOne(id);
        entity.setIsRelease("1");
        entity.setReleaseTime(CreateTimeUtils.getCreateTime());
        this.save(entity);
    }

    //禁用
    @Override
    public void doDisabled(String id) {
        JwEducationInfoEntity entity = this.findOne(id);
        entity.setIsRelease("2");
        this.save(entity);
    }

    @Override
    public void doTop(String id) {
        JwEducationInfoEntity entity = this.findOne(id);
        Integer favorite = find();
        entity.setFavorites(favorite+1);
        entity.setStauts("1");
        this.save(entity);
    }

    @Override
    public void cancelTop(String id) {
        JwEducationInfoEntity entity = this.findOne(id);
        entity.setFavorites(0);
        entity.setStauts("0");
        this.save(entity);
    }

    @Override
    public Integer find() {
        return this.repository.find();
    }
}
